package com.sky.loveshop.controller;

import com.sky.loveshop.entity.Result;
import com.sky.loveshop.entity.User;
import com.sky.loveshop.entity.common.PageResult;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.entity.exception.BizExceptionEnum;
import com.sky.loveshop.entity.qo.UserQO;
import com.sky.loveshop.entity.qo.UserRequest;
import com.sky.loveshop.entity.vo.UserVO;
import com.sky.loveshop.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/8/18 0:04
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/listUserPages")
    public Result<PageResult<UserVO>> listUserPages(@RequestBody UserQO userQO) {
        return Result.page(userService.listUserPages(userQO));
    }

    @PostMapping("/listUser")
    public Result<List<UserVO>> listUser(@RequestBody UserQO userQO) {
        return Result.success(userService.listUser(userQO));
    }

    @PutMapping("/addUser")
    public Result<Integer> addUser(@RequestBody @Valid User user) {
        return Result.success(userService.addUser(user));
    }

    @DeleteMapping("/delUserById")
    public Result<Integer> delUserById(Integer id) throws BizException {
        if (id == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(userService.delUserById(id));
    }

    @PostMapping("/editUser")
    public Result<Integer> editUser(@RequestBody User user) throws BizException {
        if (user.getId() == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(userService.updateUserById(user));
    }

    @GetMapping("/getUserById")
    public Result<UserVO> getUserById(Integer id) throws BizException {
        if (id == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(userService.getUserById(id));
    }

    @PostMapping("/login")
    public Result<UserVO> login(@RequestBody UserQO user) throws BizException {
        if (StringUtils.isEmpty(user.getPhone())) {
            throw new BizException(BizExceptionEnum.PHONE_MUST_NOT_NULL);
        }
        if (StringUtils.isEmpty(user.getPassWord())) {
            throw new BizException(BizExceptionEnum.PASSWORD_MUST_NOT_NULL);
        }
        return Result.success(userService.login(user));
    }

    /**
     * 用户绑定商店/修改绑定商店
     */
    @PostMapping("/userBindShopUpdate")
    public Result<Integer> userBindShopUpdate(@RequestBody @Valid UserRequest.BindParam req) {
        return Result.success(userService.userBindShopUpdate(req));
    }

}
